package com.vchello.thead;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.vchello.bean.file.File;
import com.vchello.bean.file.Folder;
import com.vchello.bean.pro.InvestNote;
import com.vchello.bean.pro.Pro;
import com.vchello.bean.pro.ProLables;
import com.vchello.bean.pro.ProMilepost;
import com.vchello.bean.pro.ProOrganization;
import com.vchello.bean.pro.ProjectChange;
import com.vchello.bean.user.User;
import com.vchello.bean.user.UserProgress;
import com.vchello.service.file.FileService;
import com.vchello.service.file.FolderService;
import com.vchello.service.project.ProjectService;
import com.vchello.service.user.UserService;

@Component
public class RemoveInvitationThread implements Runnable{
	
	@Autowired
	private ProjectService projectService; 
	@Autowired
	private UserService userService;
	@Autowired
	private FolderService folderService;
	@Autowired
	private FileService fileService;

	private int investorId;
	
	private int organizetionId;
	
	@Override
	public void run() {
		List<ProjectChange> projectChanges = projectService.queryProjectChanges(investorId);
		
		User organizetion = userService.findByOne(organizetionId);
		User user = userService.findByOne(investorId);
		
		//List<Object[]> progressList = projectService.findHasProgress(investorId);
		
		if(projectChanges.size()>0){
			//1.把历史进度转化为进度
			//首先建立父级
			UserProgress parentUp = new UserProgress(organizetion.getUserName(), projectService.queryProgressSort(user.getId()), 
					(long)user.getId(), 2);
			boolean flag =projectService.saveOrUpdateProgress(parentUp);
			/*if(flag){
				for(Object[] obj : progressList){
					UserProgress up = new UserProgress((String)obj[1], projectService.queryProgressSort(user.getId()), 
							(long)user.getId(), parentUp.getId(), 2,(int)obj[0]);
					projectService.saveOrUpdateProgress(up);
				}
			}*/
			//2.备份项目
			for(ProjectChange pc : projectChanges){
				Pro oldPro = projectService.findProjectById(pc.getProjectId());
				Pro pro = new Pro(oldPro.getName(), investorId, oldPro.getLogo(), oldPro.getCreateTime(), oldPro.getLink(), 
						oldPro.getBp(), oldPro.getStock(), oldPro.getMoney(), oldPro.getCurrency(), oldPro.getUnit(), 
						oldPro.getContactsName(), oldPro.getContactsPhone(), oldPro.getType(), 
						oldPro.getHide(), oldPro.getProDetail(), oldPro.getAreaId(), oldPro.getRoundId(), 
						parentUp.getId(), oldPro.getFieldId(),oldPro.getCompany());
				boolean flag_pro = projectService.saveOrUpdate(pro);
				if(flag_pro){
					//备份标签
					List<ProLables>	lables = projectService.findProLablesByProId(oldPro.getId());
					if(lables!=null&&lables.size()>0){
						List<ProLables> new_lables = new ArrayList<ProLables>();
						for(ProLables proLable : lables){
							new_lables.add(new ProLables(proLable.getName(), pro.getId()));
						}
						projectService.saveProLabels(new_lables);
					}
					//备份项目笔记
					List<InvestNote> notes = projectService.findInvestNoteByProId(oldPro.getId());
					if(notes.size()>0){
						for(InvestNote note : notes){
							projectService.saveOrUpdateNote(new InvestNote(pro.getId(), investorId, note.getCreateTime(), note.getUpdateTime(), note.getContent()));
						}
					}
					//备份里程
					List<ProMilepost> miles = projectService.findProMilepostByProId(oldPro.getId());
					if(miles.size()>0){
						List<ProMilepost> new_miles = new ArrayList<ProMilepost>();
						for(ProMilepost mile : miles){
							new_miles.add(new ProMilepost(mile.getTime(), mile.getDetail(), pro.getId()));
						}
						projectService.updateMilepost(new_miles);
					}
					//备份人员
					List<ProOrganization> organizes = projectService.findProOrganizationByProId(oldPro.getId());
					if(organizes.size()>0){
						List<ProOrganization> new_organizes = new ArrayList<ProOrganization>();
						for(ProOrganization oraganize : organizes){
							new_organizes.add(new ProOrganization(oraganize.getName(), oraganize.getPosition(), oraganize.getDetail(), oraganize.getPhone(), pro.getId()));
						}
						projectService.updateOrganizations(new_organizes);
					}
					//备份其他文件
					Folder folder = folderService.findByProjectIdAndParentIdAndUserId(pro.getId(), folderService.findProjectFoldByUserId(pro.getUserId()).getId(), pro.getUserId());
					List<File> files = fileService.findFileByFid(folder.getId());
					if(files.size()>0){
						for(File file :files){
							File new_file = new File(folder.getId(), file.getName(), file.getSize(), file.getOriginalName(), file.getUrl(), file.getCreateTime());
							fileService.saveFile(new_file);
						}
					}
					//备份尽调文件
					Folder investigateFolder = folderService.findByProjectIdAndParentIdAndUserId(pro.getId(), folderService.findProjectInvestigateFoldByUserId(pro.getUserId()).getId(), pro.getUserId());
					List<File> investigateFiles = fileService.findFileByFid(folder.getId());
					if(investigateFiles.size()>0){
						for(File file : investigateFiles){
							File new_file = new File(investigateFolder.getId(), file.getName(), file.getSize(), file.getOriginalName(), file.getUrl(), file.getCreateTime());
							fileService.saveFile(new_file);
						}
					}
				}
			}
			//3.删除ProjectChange
			projectService.deleteProjectChange(projectChanges);
		}
	}

	public ProjectService getProjectService() {
		return projectService;
	}

	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}

	public int getInvestorId() {
		return investorId;
	}

	public void setInvestorId(int investorId) {
		this.investorId = investorId;
	}

	public int getOrganizetionId() {
		return organizetionId;
	}

	public void setOrganizetionId(int organizetionId) {
		this.organizetionId = organizetionId;
	}
}
